package com.xpjhs.user.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xpjhs.role.dao.RoleDao;
import com.xpjhs.role.model.Role;
import com.xpjhs.user.dao.UserDao;
import com.xpjhs.user.model.User;

/**
 * 
 * @ClassName:UserService
 * @Description:{todo
 * @author luckyboyguo@126.com
 * @date:2015年1月16日上午11:44:02
 */
@Service
@Transactional(readOnly = true)
public class UserService {

	@Autowired
	private UserDao userDao;
	@Autowired
	private RoleDao roleDao;

	/**
	 * 
	 * @Title:listUserPage
	 * @Description:TODO
	 * @param:@param pageable
	 * @param:@return
	 * @return Page<User>
	 * @throws
	 * @param pageable
	 * @return
	 */
	public Page<User> listUserPage(Pageable pageable) {
		return userDao.findAll(pageable);

	}

	/**
	 * 
	 * @Title:saveUser
	 * @Description:保存用户
	 * @param:@param user
	 * @return void
	 * @throws
	 * @param user
	 */
	public void saveUser(User user) {
		userDao.save(user);
	}

	/**
	 * 
	 * @Title:deleteUserById
	 * @Description:根据ID删除用户
	 * @param:@param id
	 * @return void
	 * @throws
	 * @param id
	 */
	public void deleteUserById(long id) {
		userDao.delete(id);
	}

	/**
	 * 
	 * @Title:deleteUser
	 * @Description:删除用户
	 * @param:@param user
	 * @return void
	 * @throws
	 * @param user
	 */
	public void deleteUser(User user) {
		userDao.delete(user);
	}

	/**
	 * 
	 * @Title:deleteAllUsers
	 * @Description:删除所有用户
	 * @param:
	 * @return void
	 * @throws
	 */
	public void deleteAllUsers() {
		userDao.deleteAll();
	}

	/**
	 * 
	 * @Title:listAllUsers
	 * @Description:显示所有用户
	 * @param:@return
	 * @return List<User>
	 * @throws
	 * @return
	 */
	public List<User> listAllUsers() {
		List<User> list = userDao.findAll();
		return list;
	}

	/**
	 * 
	 * @Title:findUserByID
	 * @Description:通过ID查找用户
	 * @param:@param id
	 * @param:@return
	 * @return User
	 * @throws
	 * @param id
	 * @return
	 */
	public User findUserByID(long id) {
		return userDao.findOne(id);
	}

	/**
	 * 
	 * @Title:findUserByUsername
	 * @Description:根据用户名查找用户
	 * @param:@param username
	 * @param:@return
	 * @return User
	 * @throws
	 * @param username
	 * @return
	 */
	public User findUserByUsername(String username) {
		return userDao.findUserByUsername(username);
	}
	public List<Role> findRolesByUsername(String username) {
		return roleDao.findByUsersUsername(username);
	}
}
